import {Component, OnInit, ViewEncapsulation, Output, EventEmitter, Input} from '@angular/core';
import {ToolsService} from "app/shared/service/tools/tools.service";
import {CalendarBtnConfig} from "@interface/index";


@Component({
  selector: 'choose-calendar',
  templateUrl: './choose-calendar.component.html',
  styleUrls: ['./choose-calendar.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class ChooseCalendarComponent implements OnInit {
  public nowDate: any;
  public _startTime: Date;
  public _endTime: Date;
  public type: string;
  public showCalendar = false;

  @Output() cancel: EventEmitter<any> = new EventEmitter();
  @Output() confirm: EventEmitter<any> = new EventEmitter();

  @Input()
  set startTime(val) {
    this._startTime = val;
    this.checkDateType();
  }

  get startTime() {
    return this._startTime;
  }

  @Input()
  set endTime(val) {
    this._endTime = val;
    this.checkDateType();
  }

  get endTime() {
    return this._endTime;
  }
  @Input() btnConfig: CalendarBtnConfig;
  constructor(
    private tools: ToolsService
  ) {
    this.nowDate = new Date();
    this.startTime = new Date();
    this.btnConfig = new CalendarBtnConfig();
   }

  checkDateType() {
    const type = this.tools.checkTime(this.startTime, this.endTime);
    switch (type) {
      case 'free' :
        this.type = 'free';
        break;
      case 'someday' :
        this.type = 'free';
        break;
      default:
        this.type = type;
        break;
    }

  }

  chooseDate(type) {
    const val = this.tools.getRankTime(type);
    this.confirmFn(val);
  }

  cancelFn() {
    this.showCalendar = false;
    this.cancel.emit();
  }

  confirmFn(val) {
    this.showCalendar = false;
    this.confirm.emit(val);
  }

  ngOnInit() {
    this.checkDateType();
  }

}
